package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TCompanyDeptMapper;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.TCompanyDept;
import com.ruoyi.system.service.ITCompanyDeptService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.support.Convert;

/**
 * 企业部门管理 服务层实现
 * 
 * @author ruoyi
 * @date 2019-02-13
 */
@Service
public class TCompanyDeptServiceImpl implements ITCompanyDeptService 
{
	@Autowired
	private TCompanyDeptMapper tCompanyDeptMapper;

	/**
     * 查询企业部门管理信息
     * 
     * @param id 企业部门管理ID
     * @return 企业部门管理信息
     */
    @Override
	public TCompanyDept selectTCompanyDeptById(Long id)
	{
	    return tCompanyDeptMapper.selectTCompanyDeptById(id);
	}
	
	/**
     * 查询企业部门管理列表
     * 
     * @param tCompanyDept 企业部门管理信息
     * @return 企业部门管理集合
     */
	@Override
	public List<TCompanyDept> selectTCompanyDeptList(TCompanyDept tCompanyDept)
	{
	    return tCompanyDeptMapper.selectTCompanyDeptList(tCompanyDept);
	}
	
    /**
     * 新增企业部门管理
     * 
     * @param tCompanyDept 企业部门管理信息
     * @return 结果
     */
	@Override
	public int insertTCompanyDept(TCompanyDept tCompanyDept)
	{
		System.out.println("createBy===="+tCompanyDept.getCreateBy());
	    return tCompanyDeptMapper.insertTCompanyDept(tCompanyDept);
	}
	
	/**
     * 修改企业部门管理
     * 
     * @param tCompanyDept 企业部门管理信息
     * @return 结果
     */
	@Override
	public int updateTCompanyDept(TCompanyDept tCompanyDept)
	{
	    return tCompanyDeptMapper.updateTCompanyDept(tCompanyDept);
	}

	/**
     * 删除企业部门管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteTCompanyDeptByIds(String ids)
	{
		return tCompanyDeptMapper.deleteTCompanyDeptByIds(Convert.toStrArray(ids));
	}
	
	/**
     * 根据部门ID查询信息
     * 
     * @param deptId 部门ID
     * @return 部门信息
     */
    @Override
    public TCompanyDept selectDeptById()
    {
        return tCompanyDeptMapper.selectDeptById();
    }
    
    /**
     * 查询部门管理树
     * 
     * @param dept 部门信息
     * @return 所有部门信息
     */
    @Override
    @DataScope(tableAlias = "d")
    public List<Map<String, Object>> selectDeptTree(TCompanyDept tCompanyDept)
    {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        List<TCompanyDept> deptList = tCompanyDeptMapper.selectTCompanyDeptList(tCompanyDept);
        trees = getTrees(deptList, false, null);
        return trees;
    }
    
    
    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @param isCheck 是否需要选中
     * @param roleDeptList 角色已存在菜单列表
     * @return
     */
    public List<Map<String, Object>> getTrees(List<TCompanyDept> deptList, boolean isCheck, List<String> roleDeptList)
    {

        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (TCompanyDept dept : deptList)
        {
        	
                Map<String, Object> deptMap = new HashMap<String, Object>();
                deptMap.put("id", dept.getId());
                deptMap.put("pId", dept.getParentId());
                deptMap.put("name", dept.getDeptName());
                deptMap.put("title", dept.getDeptName());
                if (isCheck)
                {
                    deptMap.put("checked", roleDeptList.contains(dept.getId() + dept.getDeptName()));
                }
                else
                {
                    deptMap.put("checked", false);
                }
                System.out.println("deptMap==="+deptMap);
                trees.add(deptMap);
            }
        return trees;
    }
	
}
